package com.example.librarymanagementproject2.web.controller;

import com.example.librarymanagementproject2.Common.config.ResultVO;
import com.example.librarymanagementproject2.Common.utils.MinioUtil;
import com.example.librarymanagementproject2.model.entity.Book;
import com.example.librarymanagementproject2.model.entity.BookCategory;
import com.example.librarymanagementproject2.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName BookController
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/27 15:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * @author: kx
     * @date: 2025/10/27 16:14
     * @param category : 图书分类信息
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 插入图书分类信息
    */
    @PutMapping("/BookCategory")
    public ResultVO<String> insertBookCategory(@RequestBody BookCategory category) {
        bookService.insertBookCategory(category);
        return ResultVO.success("图书分类信息添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/27 19:23
     * @param book : 图书信息
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 插入图书信息
    */
    @PutMapping("/Book")
    public ResultVO<String> insertBook(@RequestPart("book") Book book, @RequestPart("image")MultipartFile image) {
        // 上传图片
        String fileName = minioUtil.uploadImage(image);
        // 设置图片名称
        book.setCoverUrl(fileName);
        bookService.insertBook(book);
        return ResultVO.success("图书信息添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/28 16:17
     * @param book : 图书信息
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 修改图书状态
    */
    @PostMapping("/BookStatus")
    public ResultVO<String> updateBookStatus(@RequestBody Book book) {
        bookService.updateBookStatus(book);
        return ResultVO.success("修改图书状态成功");
    }
}
